package com.ruicar.afs.cloud.afscase.paramconfmanagement.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.common.utils.Const;
import com.ruicar.afs.cloud.afscase.paramconfmanagement.entity.CaseConfParam;
import com.ruicar.afs.cloud.afscase.paramconfmanagement.mapper.CaseConfParamMapper;
import com.ruicar.afs.cloud.afscase.paramconfmanagement.service.CaseConfParamService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Optional;

/**
 * <p>
 * Description:
 * </p>
 *
 * @author bh.xia
 * @date 2020/6/4 10:20
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class CaseConfParamServiceImpl extends ServiceImpl<CaseConfParamMapper, CaseConfParam> implements CaseConfParamService {
     /**
      * @description: 根据参数标签开关判断
      * @author jiaqi.guo
      * @created 2020/7/30 17:29
      * @version 1.0
      */
    public boolean  getIsOpen(String paramLogo){
        CaseConfParam confParam= this.getOne(Wrappers.<CaseConfParam>query().lambda()
                    .eq(CaseConfParam::getParamLogo,paramLogo)
                    .eq(CaseConfParam::getParamStatus,WhetherEnum.YES.getCode()));
        if(ObjectUtils.isNotEmpty(confParam)){
            return true;
        }else{
            return false;
        }

    }

    /**
     * @Description:
     * @Author: fangchenliang
     * @Date: 2020/8/22 0:26
     * @Param: [caseConfParam]
     * @Return: boolean
     */
    public IResponse saveParam(CaseConfParam caseConfParam){
        CaseConfParam confParam= this.getOne(Wrappers.<CaseConfParam>query().lambda()
                .eq(CaseConfParam::getParamType,caseConfParam.getParamType()),false);
        if(ObjectUtils.isEmpty(confParam)){
            this.save(caseConfParam);
            return IResponse.success(true).setMsg("保存成功");
        }else{
            return IResponse.fail(caseConfParam.getParamType()+"参数类型已存在");
        }
    }

    @Override
    public boolean checkSwitch(String switchParamTypeName) {
        Optional<CaseConfParam> optional = Optional.ofNullable(getOne(
                Wrappers.<CaseConfParam>lambdaQuery().eq(CaseConfParam::getParamType, switchParamTypeName)
                        .eq(CaseConfParam::getParamValue, WhetherEnum.YES.getCode())
                        .eq(CaseConfParam::getParamStatus,WhetherEnum.YES.getCode())
        ));
        return optional.isPresent();
    }

}
